package com.ghsc.ghxc.jsb.ui.home

import android.graphics.Paint
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.cn.appcore.base.BaseVMFragment
import com.cn.appcore.config.AppConfig
import com.cn.appcore.ext.setOnFastClickListener
import com.cn.appcore.ext.showToast
import com.cn.appcore.ext.startKtxActivity
import com.cn.appcore.utils.GlideUtil
import com.cn.appcore.utils.GsonUtil
import com.cn.appcore.widget.CustomRefreshListener
import com.cn.appcore.widget.RefreshHelper
import com.flyco.roundview.RoundLinearLayout
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.config.CustKey
import com.ghsc.ghxc.jsb.config.LoginHelper
import com.ghsc.ghxc.jsb.databinding.FragmentPageListBinding
import com.ghsc.ghxc.jsb.ui.home.activity.GoodsDetailActivity
import com.ghsc.ghxc.jsb.ui.home.bean.GoodBeanData
import com.ghsc.ghxc.jsb.utils.StringUtil
import com.ghsc.ghxc.jsb.view.NotDataView
import com.makeramen.roundedimageview.RoundedImageView
import com.scwang.smart.refresh.layout.api.RefreshLayout

/**
 * 项目：GuoHuaJsb
 * 包名：com.ghsc.ghxc.jsb.ui.home
 * 类名：ListTypesFragment
 * 创建时间：2024/4/24 14:21
 * 创建人 @author： xiaolin
 * 描述 ：Todo...首页其他分类
 */
class ListTypesFragment  : BaseVMFragment<HomeViewModel, FragmentPageListBinding>(R.layout.fragment_page_list){
    override fun attachVMClass(): Class<HomeViewModel>? {
       return HomeViewModel::class.java
    }

    override fun lazyLoad() {
    }

    var typeId:Int =0
    companion object {
        fun getInstance(typeId:Int): ListTypesFragment? {
            var fragment= ListTypesFragment()
            fragment.typeId=typeId
            return fragment
        }
    }


    private val mAdapter: ListAdapter by lazy { ListAdapter() }
    var mList = arrayListOf<GoodBeanData>()
    private var notDataView: View? = null


    override fun initView(savedInstanceState: Bundle?) {
        binding.rvList.layoutManager = StaggeredGridLayoutManager(2,
            StaggeredGridLayoutManager.VERTICAL)
        binding.rvList.adapter = mAdapter
        mAdapter.setNewInstance(mList)
        notDataView= NotDataView.notDataView(requireActivity(), binding.rvList,binding.refreshLayout,"暂无商品")
        initRefreshLayout()
        pageNo = 1
        getListData()
    }

    override fun initClickListener() {
        super.initClickListener()
        mAdapter.setOnFastClickListener { data, position ->
            startKtxActivity<GoodsDetailActivity>(
                value =  "pid" to mAdapter.getItem(position).pid
            )
        }
    }

    private fun initRefreshLayout() {
        RefreshHelper.initRefreshLayout(binding.refreshLayout, object : CustomRefreshListener {
            override fun _onRefresh(refreshLayout: RefreshLayout?) {
                pageNo = 1
                getListData()
            }

            override fun _onLoadMore(refreshLayout: RefreshLayout?) {
                pageNo++
                getListData()
            }
        })
    }

    private fun getListData() {
        val map: HashMap<String, Any> = HashMap()
        map["footcat"] =""
        map["cid"] =typeId
        map["pindex"] =pageNo
        map["psize"] =pageSize
        GsonUtil.mapToJsonStrObjEncryption(map)?.let { it1 -> mViewModel.onGoodsListMethod(it1) }
    }

    override fun initRequestObserver() {
        super.initRequestObserver()
        mViewModel.apply {
            errorMsg.observe(this@ListTypesFragment) {
                showToast(it)
            }
            againLoginMsg.observe(this@ListTypesFragment) {
                showToast(CustKey.IN_AGAIN_MSG)
                LoginHelper.startLoginPage()
            }

            onGoodsListData.observe(this@ListTypesFragment) {
                RefreshHelper.finishRefresh(binding.refreshLayout, pageNo)
                if (pageNo == 1) {
                    if (it.data.list.isNullOrEmpty()){
                        mAdapter.replaceData(it.data.list)
                        mAdapter.setEmptyView(notDataView!!)
                    }
                    else {
                        mAdapter.replaceData(it.data.list)
                    }
                } else {
                    if (!it.data.list.isNullOrEmpty())
                        mAdapter.addData(it.data.list)
                }
            }
        }
    }


    //屏幕的宽度
    var widthPixels= AppConfig.getApplication().resources.displayMetrics.widthPixels
    //屏幕的密度
    var density= AppConfig.getApplication().resources.displayMetrics.density
    inner class ListAdapter    : BaseQuickAdapter<GoodBeanData, BaseViewHolder>(
        R.layout.layout_home_recommend_item) {
        val width=(widthPixels/density)
        override fun convert(holder: BaseViewHolder, item: GoodBeanData) {
            var llItem= holder.getView<RoundLinearLayout>(R.id.llItem)
            val para1 = llItem.layoutParams
            para1.width = StringUtil.dp2px((width/2)).toInt()  - StringUtil.dp2px(13f).toInt()
            llItem.layoutParams = para1

            val image = holder.getView<RoundedImageView>(R.id.image)
            GlideUtil.ImageLoad(image.context,item.image11,image)
            val img = image.layoutParams
            if (holder.position%2 == 0){
                img.height = StringUtil.dp2px(165f).toInt()
                image.layoutParams=img
            }else{
                img.height = StringUtil.dp2px(180f).toInt()
                image.layoutParams=img
            }
            holder.setText(R.id.tvTitle,item.title)
                .setText(R.id.tvPrice,item.price.toString())
                .setText(R.id.tvOriprice,"￥"+item.oriprice.toString())
            holder.getView<TextView>(R.id.tvOriprice).paint.flags = Paint.STRIKE_THRU_TEXT_FLAG
        }
    }


}